/**
 * Copyright 2006 Brad Reynolds
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.leeloo.databinding.ui.descriptor;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.leeloo.internal.core.ObjectDescriptor;

/**
 * Describes an IDiff.
 * 
 * @author Brad Reynolds
 *
 */
public class DiffDescriptor extends ObjectDescriptor {
    private static final Pattern classNamePattern = Pattern.compile("((?:\\w+\\.)+([A-Z][a-zA-Z_0-9_$]+){1})"); //$NON-NLS-1$
    
	private String toString;
    private String cleanToString;
	
	public DiffDescriptor() {
	}
	
	public String getToString() {
		return toString;
	}

	/**
	 * @param toString the toString to set
	 */
	public void setToString(String toString) {
		this.toString = toString;
	}
    
    /**
     * @return cleaned up (shortened class names) {@link #getToString()}.
     */
    public String getCleanToString() {
        if (cleanToString == null) {
            Matcher matcher = classNamePattern.matcher(toString);
            boolean match = matcher.find();
            if (match && matcher.groupCount() == 2) {
                StringBuffer buffer = new StringBuffer();
                buffer.append(toString.substring(0, matcher.start(1)));
                buffer.append(matcher.group(2));
                buffer.append(toString.substring(matcher.end(1), toString.length()));
                
                cleanToString = buffer.toString();
            } else {
                cleanToString = ""; //$NON-NLS-1$
            }
        }
        
        return cleanToString;
    }
}